package wta.cloud.service;

import java.util.Arrays;

public class Risk {
	
	//风险因子 每一行分别表示 age0_sex0 age1_sex0 age2_sex0   age0_sex1 age1_sex1 age2_sex1
	private static final double[][] riskP ={
			{0.157,0.076,0.518,0.12,0.004,0.159,0.018,0.016,0.14,0.074,0.16,0.011},
			{0.382,0.125,0.6,0.3,0.003,0.21,0.071,0.053,0.42,0.26,0.6,0.012},
			{0.425,0.222,0.556,0.556,0.005,0.246,0.145,0.14,0.58,0.372,0.522,0.024},
			{0.407,0.251,0.526,0.132,0.06,0.291,0.054,0.032,0.322,0.404,0.819,0.31},
			{0.497,0.243,0.614,0.402,0.129,0.358,0.156,0.143,0.431,0.489,0.614,0.385},
			{0.497,0.139,0.497,0.561,0.035,0.243,0.179,0.197,0.399,0.347,0.341,0.179}
	};
	private static final double[] RC = {1.76,3.21,1.44,6.2,1.71,1.24,1.75,2.09,1.32,1.45,1.17,1.2};
	
	//Other里的各项，为0表示此项没事，为1表示为致病的可能项
	public static double getRisk(int sex, int age, double bmi, int[] other) {
	//System.out.println(sex+" " +age+" " +bmi+" " + Arrays.toString(other)+" ");
		
		int line = getLine(sex, age);
		
		if (bmi < 24){
			other[0] = 0; other[1] = 0;
		}else if (bmi < 28){
			other[0] = 1; other[1] = 0;
		}else{
			other[0] = 0; other[1] = 1;
		}
		
		if (other[7] == 1) other[6] = 0;
		
		double fenmu = 1;
		for (int i = 0; i < other.length; i++) {
			fenmu *= (riskP[line][i] * RC[i] + 1 - riskP[line][i]);
		}
		
		double fenzi = 1;
		for (int i = 0; i < other.length; i++) {
			if (other[i] == 1) { // 如果 ==1 才处理，否则不处理
				fenzi *= RC[i];
			}
		}

		double result = fenzi / fenmu;
		return result;
	}
	
	public static double getMinRisk(int sex, int age, double bmi, int[] other) {
		if (bmi > 24) bmi = 23.9;
		
		int[] data = Arrays.copyOf(other, other.length);
		data[4] = 0;
		data[5] = 0;
		data[10] = 0;
		data[11] = 0;
		return getRisk(sex, age, bmi, data);
	}
	
	/**
	 * 获得用户应该在哪个行上
	 * @param sex
	 * @param age
	 * @return
	 */
	private static int getLine(int sex, int age){
		int r = 0;
		if (age <= 39) r = 0;
		else if (age >= 40 && age <= 59) r = 1;
		else if (age >= 60) r = 2;
		
		if (sex == 1)//男
			r += 3;
		return r;
	}	
	
	public static void main(String[] args) {
		
	}
	
	public static String getRiskTip(double v){
		if (v < 0.5) return "显著低于一般人群";
		if (v < 0.9) return "低于一般人群";
		if (v < 1.1) return "相当于一般人群";
		if (v < 2.0) return "高于一般人群";
		             return "显著高于一般人群";
	}

}
